package ywpt.service.contract;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientInfo;
import ywpt.model.ContractInfo;
import ywpt.model.ContractType;
import ywpt.model.Department;
import ywpt.model.ProjectInfo;
import ywpt.web.common.PublicFunction;

@Service("contractService")
@RemoteProxy(name="contractService")
public class ContractServiceImpl extends HibernateEntityDao<ContractInfo> implements ContractService{
	public ContractServiceImpl(){
		
	}


	@Override
	public String creatContractCode() {
		String str = "WXG";
		SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMM");
		DetachedCriteria query = DetachedCriteria.forClass(ContractInfo.class,"contractInfo");
		query.add(Restrictions.like("contractCode",str+FORMAT.format(new Date())+"%"));
		String contractCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ContractInfo> contractInfos = this.getContractInfos(query);
		if ( contractInfos.iterator().hasNext() )	
		{
			code = (contractInfos.iterator().next().getContractCode()).substring(9);
			maxId = new Integer(code);
		}
		contractCode = PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return str+FORMAT.format(new Date())+contractCode;
	}
	
	
	@Override
	public ContractInfo getContractInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getContractInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContractInfo> getContractInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<ContractInfo> getContractInfos() {
		return this.getAll();
	}

	@Override
	public void saveContractInfo(ContractInfo contractInfo, Long departId,
			Long typeId, Long clientId,Long proId) {
		if( departId != null && typeId != null && clientId != null ){
			contractInfo.setDepart(this.get(Department.class, departId));			
			contractInfo.setType(this.get(ContractType.class,typeId));
			contractInfo.setClient(this.get(ClientInfo.class,clientId));
		}
		if (proId!=null) {
			contractInfo.setProject(this.get(ProjectInfo.class, proId));
		}
		super.save(contractInfo);
	}

	@Override
	public void updateContractInfo(ContractInfo contractInfo, Long departId,
			Long typeId, Long clientId,Long proId) {
		if( departId != null && typeId != null && clientId != null ){
			contractInfo.setDepart(this.get(Department.class, departId));			
			contractInfo.setType(this.get(ContractType.class,typeId));
			contractInfo.setClient(this.get(ClientInfo.class,clientId));
		}
		if (proId!=null) {
			contractInfo.setProject(this.get(ProjectInfo.class, proId));
		}
		this.update(contractInfo);
	}

	@Override
	@RemoteMethod
	public int getContractInfoCountByCode(String contractCode) {
		DetachedCriteria query = DetachedCriteria.forClass(ContractInfo.class, "contractInfo");
		if ( contractCode != null && !contractCode.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("contractCode", "" + contractCode + "" ),  
							          Restrictions.like("contractCode", ""+ contractCode + "" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@Override
	public void update(ContractInfo contractInfo) {
		this.getHibernateTemplate().save(contractInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDepartments() {
		String hql="select distinct(depart) from ContractInfo";
		return this.find(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContractType> getContractTypes() {
		String hql="select distinct(type) from ContractInfo";
		return this.find(hql);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<ContractInfo> getContractInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}


	@Override
	public void removeContractInfo(Long id) {
		this.removeById(id);
	}


	@Override
	public boolean checkContractCode(String contractCode) {
		DetachedCriteria query= DetachedCriteria.forClass(ContractInfo.class);
			query.add(Restrictions.like("contractCode", "%"+contractCode+"%"));
			query.add(Restrictions.eq("deleteFlag", true));
			if (getCountByCriteria(query)>0) {
				return false;
			}else {
				return true;
			}
	}

	
	
}
